<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <FormItem
                        type="remoteSelect"
                        prop="user_id"
                        label="用户"
                        v-model="baTable.form.items!.user_id"
                        :placeholder="t('Click select')"
                        :input-attr="{
                            pk: 'id',
                            field: 'nickname',
                            // 搜索字段
                            searchField: 'nickname',
                            remoteUrl: 'user/user/index',
                            // onChange: getAdd,
                        }"
                    />
                    <el-form-item label="金额">
                        <el-input v-model="baTable.form.items!.money" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="手续费">
                        <el-input v-model="baTable.form.items!.handingfee" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="税费">
                        <el-input v-model="baTable.form.items!.taxes" type="number"></el-input>
                    </el-form-item>
                    <el-form-item label="最终到账">
                        <span style="color: red">
                            ￥{{ (baTable.form.items!.money - baTable.form.items!.handingfee - baTable.form.items!.taxes).toFixed(2) }}</span>
                        <!-- <el-input v-model="baTable.form.items!.actual_amount" readonly type="number"></el-input> -->
                    </el-form-item>
                    <FormItem
                        label="类型"
                        v-model="baTable.form.items!.type"
                        type="radio"
                        :input-attr="{
                            border: true,
                            content: { alipay: '支付宝', bank_card: '银行卡', wechat: '微信' },
                        }"
                    />
                    <el-form-item label="提现账户">
                        <el-input v-model="baTable.form.items!.account"></el-input>
                    </el-form-item>
                    <el-form-item label="开户行">
                        <el-input v-model="baTable.form.items!.withdrawal_bank"></el-input>
                    </el-form-item>
                     <el-form-item label="真实姓名">
                        <el-input v-model="baTable.form.items!.name"></el-input>
                    </el-form-item>
                    <el-form-item label="备注">
                        <el-input v-model="baTable.form.items!.memo"></el-input>
                    </el-form-item>
                    <el-form-item label="订单号">
                        <el-input readonly v-model="baTable.form.items!.orderid"></el-input>
                    </el-form-item>
                    <el-form-item label="流水号">
                        <el-input readonly v-model="baTable.form.items!.transactionid"></el-input>
                    </el-form-item>
                    <FormItem
                        :label="t('State')"
                        v-model="baTable.form.items!.status"
                        type="radio"
                        :input-attr="{
                            border: true,
                            disabled: statusDisabled,
                            content: statusList,
                        }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, inject, useTemplateRef, computed, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import type { FormItemRule } from 'element-plus'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = useTemplateRef('formRef')
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({})
const status = ref('created')

const statusList = computed(() => {
    if (status.value === 'created') {
        return { successed: '成功', rejected: '拒绝' }
    } else {
        return { created: '申请中', successed: '成功', rejected: '拒绝', confirmed: '待确认' }
    }
})

const statusDisabled = computed(() => {
    return status.value !== 'created'
})
defineExpose({
    status,
})
</script>

<style scoped lang="scss">
.avatar-uploader {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: var(--el-border-radius-small);
    box-shadow: var(--el-box-shadow-light);
    border: 1px dashed var(--el-border-color);
    cursor: pointer;
    overflow: hidden;
    width: 110px;
    height: 110px;
}
.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}
.avatar {
    width: 110px;
    height: 110px;
    display: block;
}
.image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
</style>
